package com.jrapider.designer;

import com.alibaba.fastjson.JSON;
import com.jrapider.compent.dict.DictData;
import com.jrapider.compent.dict.IDictProvider;
import com.jrapider.om.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.*;

@Component("design")
@Log4j2
public class DesignerConstants implements IDictProvider {
    private static Map<String, List<DictData>> data = new HashMap<>();
    public static final String DICT_BTNCSS = "BTNCSS";

    public static final String DICT_BTNCSS_0 = "";
    public static final String DICT_BTNCSS_1 = "layui-btn-primary";
    public static final String DICT_BTNCSS_2 = "layui-btn-normal";
    public static final String DICT_BTNCSS_3 = "layui-btn-warm";
    public static final String DICT_BTNCSS_4 = "layui-btn-danger";
    public static final String DICT_BTNCSS_5 = "layui-btn-disabled";

    public static final String DICT_BTNCSS_0_CN = "无";
    public static final String DICT_BTNCSS_1_CN = "原始";
    public static final String DICT_BTNCSS_2_CN = "百搭";
    public static final String DICT_BTNCSS_3_CN = "暖色";
    public static final String DICT_BTNCSS_4_CN = "警告";
    public static final String DICT_BTNCSS_5_CN = "禁用";


    public static final String DICT_PAGETYPE = "PAGETYPE";

    public static final String DICT_PAGETYPE_1 = "1";
    public static final String DICT_PAGETYPE_2 = "2";
    public static final String DICT_PAGETYPE_3 = "3";

    public static final String DICT_PAGETYPE_1_CN = "查询页";
    public static final String DICT_PAGETYPE_2_CN = "编辑页";
    public static final String DICT_PAGETYPE_3_CN = "查看页";


    public static final String DICT_EDITTYPE = "EDITTYPE";

    public static final String DICT_EDITTYPE_1 = "TEXT";
    public static final String DICT_EDITTYPE_2 = "MULI_TEXT";
    public static final String DICT_EDITTYPE_3 = "SELECT";
    public static final String DICT_EDITTYPE_4 = "ASYNC_SELECT";
    public static final String DICT_EDITTYPE_5 = "MULI_SELECT";
    public static final String DICT_EDITTYPE_6 = "ASYNC_MULI_SELECT";
    public static final String DICT_EDITTYPE_7 = "DATE";
    public static final String DICT_EDITTYPE_8 = "DATETIME";
    public static final String DICT_EDITTYPE_9 = "DATE_RANGE";
    public static final String DICT_EDITTYPE_10 = "SWITCH";
    public static final String DICT_EDITTYPE_11 = "RADIO";

    public static final String DICT_EDITTYPE_1_CN = "单行文本框";
    public static final String DICT_EDITTYPE_2_CN = "多行文本框";
    public static final String DICT_EDITTYPE_3_CN = "单选框";
    public static final String DICT_EDITTYPE_4_CN = "异步单选框";
    public static final String DICT_EDITTYPE_5_CN = "多选框";
    public static final String DICT_EDITTYPE_6_CN = "异步多选框";
    public static final String DICT_EDITTYPE_7_CN = "日期";
    public static final String DICT_EDITTYPE_8_CN = "日期时间";
    public static final String DICT_EDITTYPE_9_CN = "范围日期";
    public static final String DICT_EDITTYPE_10_CN = "switch开关";
    public static final String DICT_EDITTYPE_11_CN = "Radio";


    public static final String DICT_VIEWTYPE = "VIEWTYPE";

    public static final String DICT_VIEWTYPE_1 = "TEXT";
    public static final String DICT_VIEWTYPE_2 = "MULI_TEXT";
    public static final String DICT_VIEWTYPE_3 = "IMAGE";

    public static final String DICT_VIEWTYPE_1_CN = "单行文本框";
    public static final String DICT_VIEWTYPE_2_CN = "多行文本框";
    public static final String DICT_VIEWTYPE_3_CN = "图片";


    static {
        Field[] fields = DesignerConstants.class.getFields();
        for (Field field : fields) {
            String name = field.getName();
            if (!name.startsWith("DICT") || name.endsWith("CN")) {
                continue;
            }
            String[] nameArrays = name.split("_");
            if (nameArrays.length != 3) {
                continue;
            }
            String type = nameArrays[1];
            List<DictData> dictDatas = data.get(type);
            if (dictDatas == null) {
                dictDatas = new ArrayList<>();
                data.put(type, dictDatas);
            }
            try {
                Object value = field.get(null);
                Field cnField = DesignerConstants.class.getField(field.getName() + "_CN");
                Object note = cnField.get(null);
                dictDatas.add(new DictData() {{
                    setCode(type);
                    setValue(value.toString());
                    setNote(note == null ? "" : note.toString());
                }});
            } catch (IllegalAccessException e) {
                log.error(e.getMessage(), e);
            } catch (NoSuchFieldException e) {
                log.error(e.getMessage(), e);
            }
        }
        log.info("设计字典=====>");
        for (String key : data.keySet()) {
            log.info(key);
            log.info(JSON.toJSONString(data.get(key)));
        }
        log.info("设计字典=====<");
    }

    @Override
    public Set<String> supportCodes() {
        return data.keySet();
    }

    @Override
    public List<DictData> getDict(String code) {
        return data.get(code);
    }

    @Override
    public List<DictData> getDict(String code, String filter, PageInfo pageInfo) {
        return data.get(code);
    }

}
